<?php
/**
 * Created by PhpStorm.
 * User: cuong_000
 * Date: 1/11/2015
 * Time: 8:19 PM
 */
class Custom_order_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    function view($data = array(), $return_count = false)
    {
        if (empty($data)) {
            return $this->db->get('custom_order')->result();
        } else {

            if (!empty($data['rows'])) {
                $this->db->limit($data['rows']);
            }

            if (!empty($data['page'])) {
                $this->db->offset($data['page']);
            }

            if (!empty($data['order_by'])) {
                $this->db->order_by($data['order_by'], $data['sort_order']);
            }

            if ($return_count) {
                return $this->db->count_all_results('custom_order');
            } else {
                return $this->db->get('custom_order')->result();
            }
        }
    }

    function insert($data = array())
    {
        $data["name"] = $this->db->escape_str($data["name"]);
        $data["email"] = $this->db->escape_str($data["email"]);
        $file_ext = $this->db->escape_str($data["content"]);
        $data["phone"] = $this->db->escape_str($data["phone"]);
        $data["message"] = $this->db->escape_str($data["message"]);
        $data["address"] = $this->db->escape_str($data["address"]);
        if (isset($data["customer_id"])) {
            $data["customer_id"] = $this->db->escape_str($data["customer_id"]);
        } else {
            $data["customer_id"] = null;
        }
        $data["name"] = htmlspecialchars($data["name"]);
        $data["email"] = htmlspecialchars($data["email"]);
        $data["phone"] = htmlspecialchars($data["phone"]);
        $data["message"] = htmlspecialchars($data["message"]);
        $data["content"] = htmlspecialchars($data["content"]);
        $data["address"] = htmlspecialchars($data["address"]);
        $data["status"] = $this->config->item('order_status');
        $datetime = date('Y-m-d H:i:s');
        $data["datetime"] = $datetime;
        $this->db->insert('custom_order', $data);
        $id = $this->db->insert_id();
        $order_number = date('U') . $id;
        $data = array('custom_order_number' => $order_number, 'content' => $order_number . $file_ext);

        //update the order with this order id
        $this->db->where('custom_order_id', $id);
        $this->db->update('custom_order', $data);

        return $order_number;
    }

    function viewdetails($id)
    {
        $this->db->where('custom_order_id', $id);
        $result = $this->db->get('custom_order');
        $order = $result->row();
        return $order;
    }

    function get_preorder_of_user($id)
    {
        $this->db->where('customer_id', $id);
        $result = $this->db->get('custom_order')->result();
        return $result;
    }

    function update($order)
    {
        $this->db->where('custom_order_id', $order->custom_order_id);
        $this->db->update('custom_order', $order);
    }

    function delete($id)
    {
        $this->db->where('custom_order_id', $id);
        $this->db->delete('custom_order');
    }
}